package com.shortlink.controller;

import com.shortlink.pojos.dto.dataobject.ResponseResult;
import com.shortlink.pojos.dto.req.GroupReqDTO;
import com.shortlink.pojos.dto.req.GroupSortReqDTO;
import com.shortlink.pojos.dto.resp.GroupQueryRespDTO;
import com.shortlink.service.IGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/group")
@Api(tags = "组别模块")
public class GroupController {
    @Autowired
    IGroupService iGroupService;

    @PostMapping("/create/{groupName}")
    @ApiOperation("创建分组")
    public ResponseResult create(@PathVariable String groupName){
        groupName= Optional.ofNullable(groupName).orElse("默认分组");
        iGroupService.createGroup(groupName);
        return ResponseResult.okResult();
    }

    /**
     * 修改分组
     * @param groupReqDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改分组")
    public ResponseResult update(@RequestBody GroupReqDTO groupReqDTO){
        iGroupService.updateGroup(groupReqDTO);
        return ResponseResult.okResult();
    }

    @PutMapping("/sort")
    @ApiOperation("排序分组")
    public ResponseResult sort(@RequestBody List<GroupSortReqDTO> groupSortReqDTOList){
        iGroupService.sortGroup(groupSortReqDTOList);
        return ResponseResult.okResult();
    }

    /**
     * 逻辑删除
     * 将组内链接逻辑删除，移至回收站
     * @param gid
     * @return
     */
    @PutMapping("/delete/{gid}")
    @ApiOperation("删除分组")
    public ResponseResult delete(@PathVariable String gid){
        iGroupService.deleteGroup(gid);
        return ResponseResult.okResult();
    }

    @GetMapping("/query")
    @ApiOperation("查询分组")
    public ResponseResult<List<GroupQueryRespDTO>> query(){
        List<GroupQueryRespDTO> groupQueryRespDTOList=iGroupService.queryGroup();
        return ResponseResult.okResult(groupQueryRespDTOList);
    }

}
